package com.system.business.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.system.business.domain.DailyTrainCarriage;
import com.system.business.domain.TrainCarriage;
import com.system.business.req.DailyTrainCarriageQueryReq;
import com.system.business.req.DailyTrainCarriageSaveReq;
import com.system.business.resp.DailyTrainCarriageQueryResp;
import com.system.business.service.DailyTrainCarriageService;
import com.system.business.mapper.DailyTrainCarriageMapper;
import com.system.business.service.TrainCarriageService;
import com.system.common.resp.PageResp;
import com.system.common.utils.SnowUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
* @author Admin
* @description 针对表【daily_train_carriage(每日车厢)】的数据库操作Service实现
* @createDate 2024-03-21 15:02:15
*/
@Service
public class DailyTrainCarriageServiceImpl extends ServiceImpl<DailyTrainCarriageMapper, DailyTrainCarriage>
    implements DailyTrainCarriageService{

    @Autowired
    private DailyTrainCarriageMapper dailyTrainCarriageMapper;
    @Autowired
    private TrainCarriageService trainCarriageService;

    @Override
    public PageResp<DailyTrainCarriageQueryResp> queryList(DailyTrainCarriageQueryReq req) {
        Page<DailyTrainCarriage> pageCondition = new Page<>(req.getPage(), req.getSize());
        QueryWrapper<DailyTrainCarriage> passengerQueryWrapper = new QueryWrapper<>();
        Page<DailyTrainCarriage> pageData = dailyTrainCarriageMapper.selectPage(pageCondition,passengerQueryWrapper);
        List<DailyTrainCarriageQueryResp> list = BeanUtil.copyToList(pageData.getRecords(), DailyTrainCarriageQueryResp.class);

        PageResp<DailyTrainCarriageQueryResp> pageResp = new PageResp<>();
        pageResp.setTotal(pageData.getTotal());
        pageResp.setList(list);
        return pageResp;
    }

    @Override
    public void save(DailyTrainCarriageSaveReq req) {
        DateTime now = DateTime.now();

        DailyTrainCarriage dailyTrainCarriage = BeanUtil.copyProperties(req, DailyTrainCarriage.class);
        if (ObjectUtil.isNull(dailyTrainCarriage.getId())) {
            dailyTrainCarriage.setId(SnowUtil.getSnowflakeNextId());
            dailyTrainCarriage.setCreateTime(now);
            dailyTrainCarriage.setUpdateTime(now);
            dailyTrainCarriageMapper.insert(dailyTrainCarriage);
        } else {
            dailyTrainCarriage.setUpdateTime(now);
            dailyTrainCarriageMapper.updateById(dailyTrainCarriage);
        }
    }

    @Transactional
    @Override
    public void genDaily(Date date, String trainCode) {

        // 删除某日某车次的车厢信息
        QueryWrapper<DailyTrainCarriage> dailyTrainCarriageQueryWrapper = new QueryWrapper<>();
        dailyTrainCarriageQueryWrapper.eq("train_code",trainCode).eq("date",date);
        dailyTrainCarriageMapper.delete(dailyTrainCarriageQueryWrapper);

        // 查出某车次的所有的车厢信息
        QueryWrapper<TrainCarriage> trainCarriageQueryWrapper = new QueryWrapper<>();
        trainCarriageQueryWrapper.eq("train_code",trainCode);
        List<TrainCarriage> carriageList = trainCarriageService.list(trainCarriageQueryWrapper);
        if (CollUtil.isEmpty(carriageList)) {
            return;
        }

        for (TrainCarriage trainCarriage : carriageList) {
            DateTime now = DateTime.now();
            DailyTrainCarriage dailyTrainCarriage = BeanUtil.copyProperties(trainCarriage, DailyTrainCarriage.class);
            dailyTrainCarriage.setId(SnowUtil.getSnowflakeNextId());
            dailyTrainCarriage.setCreateTime(now);
            dailyTrainCarriage.setUpdateTime(now);
            dailyTrainCarriage.setDate(date);
            dailyTrainCarriageMapper.insert(dailyTrainCarriage);
        }
    }

    @Override
    public List<DailyTrainCarriage> selectBySeatType (Date date, String trainCode, String seatType) {
        QueryWrapper<DailyTrainCarriage> dailyTrainCarriageQueryWrapper = new QueryWrapper<>();
        dailyTrainCarriageQueryWrapper.eq("date",date)
                .eq("train_code",trainCode).eq("seat_type",seatType);
        return dailyTrainCarriageMapper.selectList(dailyTrainCarriageQueryWrapper);
    }
}




